package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.CPReportService;
import com.yixun.qfbao.apidto.UpdateAdviserReqDto;
import com.yixun.qfbao.apidto.UpdateAdviserResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.vo.UpdateAdviserReqVo;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.CustomerPremisesService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UpdateAdviserHander extends CommonAbstractHander {
    private static final Logger logger = LoggerFactory.getLogger(UpdateAdviserHander.class);

    @Autowired
    private CustomerPremisesService customerPremisesService;
    @Autowired
    private CPReportService cpReportService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO baseRequestDTO) {
        UpdateAdviserReqVo reqVo = (UpdateAdviserReqVo) baseRequestDTO;
        UpdateAdviserReqDto reqDto =  reqVo.getParams();
        BaseResponseDTO<UpdateAdviserResDto> res = new BaseResponseDTO<>();
        UpdateAdviserResDto resDto = new UpdateAdviserResDto();
        try {
            if(StringUtils.isBlank(reqDto.getRecommendedChannelId())) {
                throw new TServerException("报备ID不能为空！");
            }
            logger.info("UpdateAdviserHander reqDto{}",reqDto);
            resDto = cpReportService.m2ToUpdateAdviser(reqDto);
        } catch (TServerException te) {
            logger.error("UpdateSaleStatusHander bus error: {}", te);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg(te.getErrorMsg());
        } catch (Exception e) {
            logger.error("UpdateSaleStatusHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("更新置业顾问失败！");
        }
        res.setResult(resDto.getResult());
        res.setResData(resDto);
        return res;
    }
}
